package org.jeecg.modules.zhuanjjslb.service.impl;

import org.jeecg.modules.zhuanjjslb.entity.Zhuanjjs;
import org.jeecg.modules.zhuanjjslb.entity.ZhuanjjsZj;
import org.jeecg.modules.zhuanjjslb.mapper.ZhuanjjsZjMapper;
import org.jeecg.modules.zhuanjjslb.mapper.ZhuanjjsMapper;
import org.jeecg.modules.zhuanjjslb.service.IZhuanjjsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 专家介绍相关科室列表
 * @Author: jeecg-boot
 * @Date:   2020-07-30
 * @Version: V1.0
 */
@Service
public class ZhuanjjsServiceImpl extends ServiceImpl<ZhuanjjsMapper, Zhuanjjs> implements IZhuanjjsService {

	@Autowired
	private ZhuanjjsMapper zhuanjjsMapper;
	@Autowired
	private ZhuanjjsZjMapper zhuanjjsZjMapper;
	
	@Override
	@Transactional
	public void saveMain(Zhuanjjs zhuanjjs, List<ZhuanjjsZj> zhuanjjsZjList) {
		zhuanjjsMapper.insert(zhuanjjs);
		if(zhuanjjsZjList!=null && zhuanjjsZjList.size()>0) {
			for(ZhuanjjsZj entity:zhuanjjsZjList) {
				//外键设置
				entity.setZhubId(zhuanjjs.getId());
				zhuanjjsZjMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(Zhuanjjs zhuanjjs,List<ZhuanjjsZj> zhuanjjsZjList) {
		zhuanjjsMapper.updateById(zhuanjjs);
		
		//1.先删除子表数据
		zhuanjjsZjMapper.deleteByMainId(zhuanjjs.getId());
		
		//2.子表数据重新插入
		if(zhuanjjsZjList!=null && zhuanjjsZjList.size()>0) {
			for(ZhuanjjsZj entity:zhuanjjsZjList) {
				//外键设置
				entity.setZhubId(zhuanjjs.getId());
				zhuanjjsZjMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		zhuanjjsZjMapper.deleteByMainId(id);
		zhuanjjsMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			zhuanjjsZjMapper.deleteByMainId(id.toString());
			zhuanjjsMapper.deleteById(id);
		}
	}
	
}
